﻿using System.Data.Entity;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.Serialization;

namespace homework10
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int currentStat = 0;    //记录当前功能状态
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            btnSchool_Click(sender, e);
        }

        //学校管理
        private void btnSchool_Click(object sender, RoutedEventArgs e)
        {
            if (currentStat == 1) return;
            currentStat = 1;
            txtTitle.Text = "学校管理";
            stcSchoolCbx.Visibility = Visibility.Collapsed;
            stpClassCbx.Visibility = Visibility.Collapsed;
            lvwSchool.Visibility = Visibility.Visible;
            lvwClass.Visibility = Visibility.Collapsed;
            lvwStudent.Visibility = Visibility.Collapsed;
            ShowSchoolInListView();
        }

        //班级管理
        private void btnClass_Click(object sender, RoutedEventArgs e)
        {
            if (currentStat == 2) return;
            currentStat = 2;
            txtTitle.Text = "班级管理";
            stcSchoolCbx.Visibility = Visibility.Visible;
            stpClassCbx.Visibility = Visibility.Collapsed;
            lvwSchool.Visibility = Visibility.Collapsed;
            lvwClass.Visibility = Visibility.Visible;
            lvwStudent.Visibility = Visibility.Collapsed;
            FillSchoolCbx();
        }

        //学生管理
        private void btnStudent_Click(object sender, RoutedEventArgs e)
        {
            if (currentStat == 3) return;
            currentStat = 3;
            txtTitle.Text = "学生管理";
            stcSchoolCbx.Visibility = Visibility.Visible;
            stpClassCbx.Visibility = Visibility.Visible;
            lvwSchool.Visibility = Visibility.Collapsed;
            lvwClass.Visibility = Visibility.Collapsed;
            lvwStudent.Visibility = Visibility.Visible;
            if (cbxSchool.Items.IsEmpty) FillSchoolCbx();
            if (cbxSchool.SelectedItem != null)
                FillClassCbx((School)cbxSchool.SelectedItem);
        }

        //操作日志
        private void btnLogs_Click(object sender, RoutedEventArgs e)
        {
            WinLogs winLogs = new WinLogs();
            winLogs.ShowDialog();
        }

        //选择学校
        private void cbxSchool_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            School school = (School)cbxSchool.SelectedItem;
            if (school != null)
            {
                switch (currentStat)
                {
                    case 2:
                        ShowClassInListView(school);
                        break;
                    case 3:
                        lvwStudent.Items.Clear();
                        FillClassCbx(school);
                        break;
                    default: break;
                }
            }
        }

        //选择班级
        private void cbxClass_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //School school = (School)cbxSchool.SelectedItem;
            Class xclass = (Class)cbxClass.SelectedItem;
            if (xclass != null)
            {
                ShowStudentInListView(xclass);
            }
        }

        //查询
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            WinQuery winQuery = new WinQuery();
            winQuery.ShowDialog();
        }

        //增加
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            switch (currentStat)
            {
                case 1:
                    WinSchoolEdit winSchoolEdit = new WinSchoolEdit();
                    winSchoolEdit.action = 1;
                    winSchoolEdit.ShowDialog();
                    ShowSchoolInListView();
                    break;

                case 2:
                    {
                        School school = (School)cbxSchool.SelectedItem;
                        if (school != null)
                        {
                            WinClassEdit winClassEdit = new WinClassEdit();
                            winClassEdit.action = 1;
                            winClassEdit.school = school;
                            winClassEdit.ShowDialog();
                            ShowClassInListView(school);
                        }
                    }
                    break;
                case 3:
                    {
                        School school = (School)cbxSchool.SelectedItem;
                        Class xclass = (Class)cbxClass.SelectedItem;
                        if ((school != null)&& (xclass != null))
                        {
                            WinStudentEdit winStudentEdit = new WinStudentEdit();
                            winStudentEdit.action = 1;
                            winStudentEdit.school = school;
                            winStudentEdit.xclass=xclass;
                            winStudentEdit.ShowDialog();
                            ShowStudentInListView(xclass);
                        }
                    }
                    break;
                default:break;
            }
        }

        //删除
        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            switch (currentStat)
            {
                case 1:
                    if (lvwSchool.SelectedItem != null)
                    {
                        School sc = (School)lvwSchool.SelectedItem;
                        if (MessageBox.Show("删除操作不可恢复，确定删除 编号:" + sc.SchoolId.ToString() + ", 校名:" + sc.Name +
                                " 的学校及其班级、学生吗？", "删除学校", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            RemoveSchool(sc);
                            ShowSchoolInListView();
                        }
                    }
                    break;

                case 2:
                    if ((cbxSchool.SelectedItem != null) && (lvwClass.SelectedItem!=null))
                    {
                        School sc = (School)cbxSchool.SelectedItem;
                        Class cls = (Class)lvwClass.SelectedItem;

                        if (MessageBox.Show("删除操作不可恢复！确定删除 "+ sc.Name+"[编号:" + sc.SchoolId.ToString() + "] " + 
                                cls.Name + "[编号:" + cls.ClassId.ToString() + "]的班及其学生吗？","删除班级", 
                                MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            RemoveClass(cls);
                            ShowClassInListView(sc);
                        }
                    }
                    break;

                case 3:
                    if ((cbxClass.SelectedItem != null) && (lvwStudent.SelectedItem != null))
                    {
                        Class cl = (Class)cbxClass.SelectedItem;
                        Student student = (Student)lvwStudent.SelectedItem;

                        if (MessageBox.Show("删除操作不可恢复！确定删除 " + cl.Name + "[编号:" + cl.ClassId.ToString() + "] 的学生" +
                                student.Name + "[编号:" + student.StudentId.ToString() + "]吗？", "删除学生",
                                MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            RemoveStudent(student);
                            ShowStudentInListView(cl);
                        }
                    }
                    break;

                default: break;
            }
        }

        //修改
        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            switch (currentStat)
            {
                case 1:
                    if (lvwSchool.SelectedItem != null)
                    {
                        WinSchoolEdit winSchoolEdit = new WinSchoolEdit();
                        winSchoolEdit.schoolToEdit = (School)lvwSchool.SelectedItem;
                        winSchoolEdit.action = 2;
                        winSchoolEdit.ShowDialog();
                        ShowSchoolInListView();
                    }
                    break;
                case 2:
                    if ((cbxSchool.SelectedItem!=null)&& (lvwClass.SelectedItem != null)) 
                    {
                        School school = new School();
                        school= (School)cbxSchool.SelectedItem;
                        WinClassEdit winClassEdit = new WinClassEdit();
                        winClassEdit.action = 2;
                        winClassEdit.school = school;
                        winClassEdit.classToEdit=(Class)lvwClass.SelectedItem;
                        winClassEdit.ShowDialog();
                        ShowClassInListView(school);
                    }

                    break;
                case 3:
                    if ((cbxClass.SelectedItem != null) && (lvwStudent.SelectedItem != null))
                    {
                        School school = (School)cbxSchool.SelectedItem;
                        Class xclass = (Class)cbxClass.SelectedItem;

                        WinStudentEdit winStudentEdit = new WinStudentEdit();
                        winStudentEdit.action = 2;
                        winStudentEdit.school = school;
                        winStudentEdit.xclass = xclass;
                        winStudentEdit.studentToEdit=(Student)lvwStudent.SelectedItem;
                        winStudentEdit.ShowDialog();
                        ShowStudentInListView(xclass);
                    }
                    break;
                default: break;
            }
        }

        //在ListView填充学校
        public void ShowSchoolInListView()
        {
            //lvwSchool.Items.Clear();
            using (var context = new AppDbContext())
            {
                context.Schools.Load();
                lvwSchool.ItemsSource = context.Schools.Local.ToObservableCollection();
            }
        }

        //在ListView填充班级
        public void ShowClassInListView(School sc)
        {
            lvwClass.Items.Clear();
            using (var context = new AppDbContext())
            {
                var query = context.Classes
                    .Where(s => s.SchoolId == sc.SchoolId);
                foreach (var s in query)
                {
                    lvwClass.Items.Add(s);
                }

                //context.Schools.Load();
                //lvwSchool.ItemsSource = context.Schools.Local.ToObservableCollection();
            }
        }

        //在ListView填充学生信息
        public void ShowStudentInListView(Class cs)
        {
            lvwStudent.Items.Clear();
            using (var context = new AppDbContext())
            {
                var query = context.Students
                    .Where(s => s.ClassId == cs.ClassId);
                foreach (var s in query)
                {
                    lvwStudent.Items.Add(s);
                }

                //context.Schools.Load();
                //lvwSchool.ItemsSource = context.Schools.Local.ToObservableCollection();
            }
        }

        //向comboBox中填充学校
        public void FillSchoolCbx()
        {
            using (var context = new AppDbContext())
            {
                context.Schools.Load();
                cbxSchool.DisplayMemberPath = "Name";
                cbxSchool.SelectedValuePath = "SchoolId";
                cbxSchool.ItemsSource = context.Schools.Local.ToObservableCollection();
            }
            if (cbxSchool.Items.Count > 0) cbxSchool.SelectedIndex = 0;
        }

        //向comboBox中填充班级
        public void FillClassCbx(School sc)
        {
            cbxClass.Items.Clear();
            using (var context = new AppDbContext())
            {
                var query = context.Classes
                    .Where(s => s.SchoolId == sc.SchoolId);
                cbxClass.DisplayMemberPath = "Name";
                cbxClass.SelectedValuePath = "ClassId";
                foreach (var s in query)
                {
                    cbxClass.Items.Add(s);
                }
            }
            if (cbxClass.Items.Count > 0) cbxClass.SelectedIndex = 0;
        }

        //删除学校
        public bool RemoveSchool(School school)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Schools.Remove(school);
                    context.SaveChanges();
                }

                DbHelper.WriteLogs("Remove school:" + school.ToString());

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //删除班级
        public bool RemoveClass(Class xclass)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    var mclass = context.Classes.Include("Students").FirstOrDefault(p => p.ClassId == xclass.ClassId);
                    if (mclass != null)
                    {
                        context.Classes.Remove(mclass);
                        context.SaveChanges();
                    }
                }
                DbHelper.WriteLogs("Remove class:" + xclass.ToString());
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        //删除学生
        public bool RemoveStudent(Student student)
        {
            try
            {
                using (var context = new AppDbContext())
                {
                    context.Students.Remove(student);
                    context.SaveChanges();
                }

                DbHelper.WriteLogs("Remove student:" + student.ToString());
                
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}